package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.example.demo.po.*;
import com.example.demo.service.TBizArtService;
import com.example.demo.service.TBizAuctionArtService;
import com.example.demo.service.TBizAuctionService;
import com.example.demo.util.CodeCache;
import com.example.demo.util.DateUtil;
import com.example.demo.util.UploadALiYunOOS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tBizAuction")
public class TBizAuctionController {
    @Autowired
    TBizAuctionService tBizAuctionService;
    @Autowired
    TBizArtService tBizArtService;
    @Autowired
    TBizAuctionArtService tBizAuctionArtService;

    @RequestMapping(value = "/list")
    public ModelAndView list(@ModelAttribute TBizAuction auction, HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        PageRequest page = new PageRequest();
        page.setPageNum(auction.getPageNum());
        page.setPageSize(auction.getPageSize());
        PageResult resilt = tBizAuctionService.findByPage(auction,page);
        List<TBizAuction> list = (List<TBizAuction>) resilt.getContent();
        if(list!=null&&list.size()>0) {
            Map<String, String> codeMap = CodeCache.codeMap;
            UploadALiYunOOS oss = new UploadALiYunOOS();
            OSSClient ossClient = oss.creatOSSClient();
            for (TBizAuction auc : list) {
                //图片转换
                if(auc.getPosterPicture()!=null){
                    String path = oss.styleQuery(ossClient,auc.getPosterPicture().toString(),"style/ys320");
                    auc.setPosterPictureShow(path);
                }
            }
        }

        view.setViewName("/auction/auction_list");
        view.addObject("result", JSON.toJSON(resilt));
        return  view;
    }

    /**
     * 根据拍卖行code获取作品
     * @param auction
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/aalist")
    public ModelAndView aalist(@ModelAttribute TBizAuction auction, HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        PageRequest page = new PageRequest();
        page.setPageNum(auction.getPageNum());
        page.setPageSize(auction.getPageSize());
        PageResult resilt = tBizArtService.findByPage(auction.getAuctionid(),page);
        List<TBizArt> list = (List<TBizArt>) resilt.getContent();
        if(list!=null&&list.size()>0){
            Map<String, String> codeMap = CodeCache.codeMap;
            UploadALiYunOOS oss = new UploadALiYunOOS();
            OSSClient ossClient =oss.creatOSSClient();
            for (TBizArt art1:list){
                String sDesc =codeMap.get("salestate"+"_"+art1.getSalestate());
                art1.setSalestate(sDesc);
                art1.setStyle(codeMap.get("style"+"_"+art1.getStyle()));
                art1.setStyle1(codeMap.get("style1"+"_"+art1.getStyle1()));
                //图片转换
                if(art1.getMainpic()!=null){
                    String path = oss.styleQuery(ossClient,art1.getMainpic().toString(),"style/ys320");
                    art1.setMainpic(path);
                }
            }
            resilt.setContent(list);
        }
        view.addObject("auctionid",auction.getAuctionid());
        view.setViewName("/auction/aa_list");
        view.addObject("result", JSON.toJSON(resilt));
        return  view;
    }


    @RequestMapping(value = "/artlist")
    public ModelAndView artlist(@ModelAttribute TBizArt art, HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        PageRequest page = new PageRequest();
        page.setPageNum(art.getPageNum());
        page.setPageSize(art.getPageSize());
        art.setSalestate("0");
        PageResult resilt = tBizArtService.findNoauc(art,page);
        List<TBizArt> list = (List<TBizArt>) resilt.getContent();
        if(list!=null&&list.size()>0){
            Map<String, String> codeMap = CodeCache.codeMap;
            UploadALiYunOOS oss = new UploadALiYunOOS();
            OSSClient ossClient =oss.creatOSSClient();
            for (TBizArt art1:list){
                String sDesc =codeMap.get("salestate"+"_"+art1.getSalestate());
                art1.setSalestate(sDesc);
                art1.setStyle(codeMap.get("style"+"_"+art1.getStyle()));
                art1.setStyle1(codeMap.get("style1"+"_"+art1.getStyle1()));
                //图片转换
                if(art1.getMainpic()!=null){
                    String path = oss.styleQuery(ossClient,art1.getMainpic().toString(),"style/ys320");
                    art1.setMainpic(path);
                }
            }
            resilt.setContent(list);
        }
        view.setViewName("/auction/art_list");
        view.addObject("auctionid",art.getAuctionid());
        view.addObject("result", JSON.toJSON(resilt));
        return  view;
    }


    @RequestMapping(value = "/editInit")
    public ModelAndView editInit(@ModelAttribute TBizAuction auction,HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        if(auction.getId()!=null){
            TBizAuction auctionInfo =tBizAuctionService.findById(auction.getId());
            //图片转换
            if(auctionInfo.getPosterPicture()!=null){
                UploadALiYunOOS oss = new UploadALiYunOOS();
                OSSClient ossClient =oss.creatOSSClient();
                String path = oss.styleQuery(ossClient,auctionInfo.getPosterPicture().toString(),"style/ys320");
                auctionInfo.setPosterPictureShow(path);
            }
            view.addObject("auc",auctionInfo);
        }
        view.setViewName("/auction/auction_edit");
        return  view;
    }

    @RequestMapping(value = "/saveOrUpdate" ,method = RequestMethod.POST)
    public String saveOrUpdate(@RequestBody TBizAuction auction, HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
             if(tBizAuctionService.saveOrUpdate(auction,request)){
                jsonObject.put("oper_res",true);
            }else{
                jsonObject.put("oper_res",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
        }
        return  jsonObject.toJSONString();
    }

    /**
     * 拍卖会作品添加准备
     * @param auctionId
     * @param artcode
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/auctionArtEdit")
    public ModelAndView auctionArtEdit(@ModelAttribute(value = "auctionId") String auctionId,@ModelAttribute(value = "artcode") String artcode,
                               HttpServletRequest request, HttpSession session){
        ModelAndView view = new ModelAndView();
        try{
            TBizAuctionArt aa  = new TBizAuctionArt();
            aa.setArtcode(artcode);
            aa.setAuctionid(auctionId);
            TBizAuctionArt taa = tBizAuctionArtService.findByAA(aa);
            view.addObject("auctionId",auctionId);
            view.addObject("artcode",artcode);
            view.addObject("taa",taa);
            view.setViewName("/auction/aa_edit");
        }catch (Exception e){
            e.printStackTrace();
        }
        return  view;
    }

    @RequestMapping(value = "/aaSave" ,method = RequestMethod.POST)
    public String aaSave(@RequestBody TBizAuctionArt aa, HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            boolean a = true;
            if(aa.getId()!=null&&!aa.getId().equals(""))
                a =tBizAuctionArtService.update(aa);
            else
                a = tBizAuctionArtService.save(aa);

            if(a){
                jsonObject.put("oper_res","success");
            }else{
                jsonObject.put("oper_res",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
        }
        return  jsonObject.toJSONString();
    }

    @RequestMapping(value = "/auctionArtDel")
    public String auctionArtDel(@ModelAttribute(value = "auctionId") String auctionId,@ModelAttribute(value = "artcode") String artcode
            , HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            TBizAuctionArt aa = new TBizAuctionArt();
            aa.setArtcode(artcode);
            aa.setAuctionid(auctionId);
            if(tBizAuctionArtService.del(aa)){
                jsonObject.put("oper_res","success");
            }else{
                jsonObject.put("oper_res",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
        }
        return  jsonObject.toJSONString();
    }

    @RequestMapping(value = "/del")
    public String del(@ModelAttribute(value = "auctionid") String auctionid
            , HttpServletRequest request, HttpSession session){
        JSONObject jsonObject = new JSONObject();
        try{
            if(tBizAuctionService.del(auctionid)){
                jsonObject.put("oper_res","success");
            }else{
                jsonObject.put("oper_res",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("oper_res",false);
        }
        return  jsonObject.toJSONString();
    }

}
